package com.bw.goods.mvvm.bind;

import android.view.LayoutInflater;
import android.view.View;

import com.bw.goods.R;
import com.bw.goods.mvvm.adapter.HomeBannerAdapter;
import com.bw.goods.mvvm.adapter.HomeHotAdapter;
import com.bw.goods.mvvm.adapter.HomeMenuAdapter;
import com.bw.goods.mvvm.adapter.HomeTopAdapter;
import com.bw.goods.mvvm.adapter.HomeTxtBannerAdapter;
import com.bw.goods.mvvm.model.entity.HomeBannerEntity;
import com.bw.goods.mvvm.model.entity.HomeMenuEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.Banner;

import java.util.List;

import androidx.databinding.BindingAdapter;
import androidx.databinding.ObservableField;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

/**
 * 针对商品列表等列表进行databinding自定义属性
 * */
public class GoodsRvBind {

    @BindingAdapter(value = {"goodsadapter","imagebanner",
            "txtbanner","menudata","hotdata","topEntities","transformer"})
    public static void bindGoodsRv(RecyclerView recyclerView,
                                   BaseQuickAdapter goodsadapter,
                                   List<HomeBannerEntity> imagebanner,
                                   List<String>txtbanner,
                                   List<HomeMenuEntity>menudata,
                                   List<String>hotdata,
                                   List<HomeBannerEntity>topEntities,
                                   ViewPager.PageTransformer transformer){
        LinearLayoutManager lmanager = new LinearLayoutManager(recyclerView.getContext());
        lmanager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(lmanager);
        View headView = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.view_home_banner,null);
        //创建顶部Banner View
        Banner home_view_banner = headView.findViewById(R.id.home_view_banner);
        home_view_banner.setAdapter(new HomeBannerAdapter(imagebanner))
                .setOrientation(Banner.HORIZONTAL)
                .start();
        //创建文字Banner View
        Banner home_text_banner = headView.findViewById(R.id.home_text_banner);
        home_text_banner.setAdapter(new HomeTxtBannerAdapter(txtbanner))
                .setOrientation(Banner.VERTICAL)
                .start();
        //创建菜单
        RecyclerView home_menu_rv = headView.findViewById(R.id.home_menu_rv);
        GridLayoutManager manager = new GridLayoutManager(recyclerView.getContext(),4);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        home_menu_rv.setLayoutManager(manager);
        HomeMenuAdapter homeMenuAdapter = new HomeMenuAdapter();
        homeMenuAdapter.setNewInstance(menudata);
        home_menu_rv.setAdapter(homeMenuAdapter);
        //创建热门水平recyclerview
        RecyclerView home_hot_rv = headView.findViewById(R.id.home_hot_rv);
        LinearLayoutManager hotManager = new LinearLayoutManager(recyclerView.getContext());
        hotManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        home_hot_rv.setLayoutManager(hotManager);
        HomeHotAdapter homeHotAdapter = new HomeHotAdapter();
        homeHotAdapter.setNewInstance(hotdata);
        home_hot_rv.setAdapter(homeHotAdapter);
        //创建画廊效果ViewPager
        ViewPager home_vp = headView.findViewById(R.id.home_vp);
        home_vp.setOffscreenPageLimit(4);
        home_vp.setPageTransformer(true,transformer);
        home_vp.setAdapter(new HomeTopAdapter(topEntities));
        goodsadapter.addHeaderView(headView);
        recyclerView.setAdapter(goodsadapter);
    }

    @BindingAdapter(value = "adapter")
    public static void bindAdapter(RecyclerView recyclerView, BaseQuickAdapter adapter){
        if (adapter == null)
            return;
        recyclerView.setAdapter(adapter);
    }

    @BindingAdapter(value = "orientation")
    public static void bindLinearManager(RecyclerView recyclerView,int orientation){
        LinearLayoutManager manager = new LinearLayoutManager(recyclerView.getContext());
        manager.setOrientation(orientation);
        recyclerView.setLayoutManager(manager);
    }

    @BindingAdapter(value = {"gridorientation","rvnum"})
    public static void bindGridManager(RecyclerView recyclerView,int gridorientation,int rvnum){
        GridLayoutManager manager = new GridLayoutManager(recyclerView.getContext(),rvnum);
        manager.setOrientation(gridorientation);
        recyclerView.setLayoutManager(manager);
    }
}
